package com.malljelly.study.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * Created by 骑着蜗牛去旅行 on 2018/8/18.
 */
@Component
@Slf4j
public class MyUserDetailsService implements UserDetailsService {

//    这个UserDetails 就是封装了用户信息的对象，里面包含了七个方法
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("登录用户名:"+username);
        //根据用户名，查找到对应的密码，与权限

//        SpringSecurity中有一个PasswordEncoder接口 对密码进行加密
        String password = new BCryptPasswordEncoder().encode("123");
        //封装用户信息，并返回。参数分别是：用户名，密码，用户权限
        return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
